// <<<<<<< HEAD:pages/xianqin/ajax.js
// function http(url, data, method) {
//   return new Promise((resolve, reject) => {
//     wx.showLoading({
//       title: '加载中...',
//     })
//     wx.request({
//       url,
//       data,
//       method: method || 'get',
//       success: function (res) {
//         resolve(res)
//       },
//       fail: function (err) {
//         reject(err)
//       },
//       complete: function (res) {
//         wx.hideLoading()
//       }
//     })
//   })
// }
// =======
function http(url,type, data, method) {
  wx.showToast({
    title: "加载中...",
    icon: "loading"
  })
  return new Promise((resolve, reject) => {
    if (type == "params") {
      wx.request({
        url: url + "?" + jointParams(data),
        method: method || 'get',
        success: function (res) {//成功
          resolve(res);
        },
        fail: function (err) {//失败
          reject(err);
        },
        complete: function (res) {//完成
          wx.hideToast();
        }
      })
    } else {
      wx.request({
        url,
        data: data,
        method: method || 'get',
        success: function (res) {//成功
          resolve(res);
        },
        fail: function (err) {//失败
          reject(err);
        },
        complete: function (res) {//完成
          wx.hideToast();
        }
      })
    }
  })
}
function jointParams(data) {//拼接params参数
  let strForm = JSON.stringify(data);
  let lastAttr = strForm.substring(strForm.lastIndexOf(",") + 2, strForm.lastIndexOf(":") - 1);
  let urlStr = "";
  for (let f in data) {
    if (lastAttr == f) {
      urlStr += f + "=" + data[f];
    } else {
      urlStr += f + "=" + data[f] + "&";
    }
  }
  return urlStr;
}
// >>>>>>> e58e51ecd41c94e2176ce22549f134750bc43e62:pages/home/js/ajax.js
module.exports = http;